angular.module('commonApp', ["ui.bootstrap", "markApp"])
.config(['$httpProvider', '$routeProvider', '$locationProvider', ($httpProvider, $routeProvider, $locationProvider) -> 
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'
    $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'
    $httpProvider.defaults.transformRequest = (data) ->
        return (if angular.isObject(data) and String(data) isnt "[object File]" then jQuery.param(data) else data)
    $routeProvider
    .when '/index/mark',
        templateUrl: "/public/tpl/mark.html"
        controller: "markCtrl"
    .when '/member',
        templateUrl: "/public/tpl/tabmember.html"
        controller: "usersetCtrl"
    .otherwise
        redirectTo: '/illust/'
    $locationProvider.html5Mode(true).hashPrefix('!')
    ])
.factory('showMsg', ['$rootScope','$timeout', ($rootScope, $timeout) ->
    (msg, time, style)->
        time = time or 3500
        $rootScope.titlemsg = msg
        $rootScope.msgstyle = style||''
        $timeout(->
            $rootScope.titlemsg = " "
        , time
        )
    ])
# 全屏
.factory('togFullScreen', ['$timeout', ($timeout) ->
    () ->
        console.log "fullScreen"
        if !document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement 
            if document.documentElement.requestFullscreen
              document.documentElement.requestFullscreen()
            else if document.documentElement.mozRequestFullScreen
              document.documentElement.mozRequestFullScreen()
            else if document.documentElement.webkitRequestFullscreen
              document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT)
        else 
            if document.cancelFullScreen
              document.cancelFullScreen()
            else if document.mozCancelFullScreen
              document.mozCancelFullScreen()
            else if document.webkitCancelFullScreen
              document.webkitCancelFullScreen()
    ])
.factory('userInfo', ['$rootScope', ($rootScope) ->
    user: $rootScope.user
    ])

.factory('update', ['$http', ($http) -> 
    (url, data, callback) -> 
        $http.post(url, data, callback).success( (bd) ->
            if callback? then callback(bd) else console.log("not cb")
            )
        ])


.factory('logout', ['$http', ($http) -> 
    (cb) ->
        # console.log("hllo")
        $http.get("/user/logout").success((data)->
            cb(data)
            )
        ])
.filter('timeFromnow',  ->
    (time) ->
        moment(time, "YYYY-MM-DD hh:mm:ss").fromNow()
    )
.controller('userCtrl', ['$scope', 'update', 'userInfo', '$timeout', '$rootScope', 'logout', ($scope, update, userInfo, $timeout, $rootScope, logout) ->
    $rootScope.global = {}
    if global.username? and global.uid?
        $rootScope.global.username = global.username
        $rootScope.global.uid = global.uid
        $rootScope.global.dimen = global.dimen
        $rootScope.logstatus = true
    $scope.nameCtrl = false
    $scope.send = (e) ->
        if e.keyCode == 13
            $scope.updateUser()
    $scope.showModal = (url) ->
        $scope.url = url
        $scope.currBtn =    if url is 'register' then "注册" else "登录" 
        $scope.modalswitch = if url is 'register' then "btn-info" else "btn-primary"
        $scope.currUrl = url
        $scope.userModal = true
    $scope.user = userInfo
    $scope.hideModal = ->
        $scope.userModal = false
    $scope.updateUser = ->
        update "/user/#{$scope.currUrl}", $scope.user, (bd) ->
            $scope.infostatus = true
            if bd.status isnt 1
                if bd.info?
                    $scope.info = "#{bd.info}:#{bd.data}"
                else 
                    $scope.info = "出现不可预知错误"
                $scope.infostyle = "alert-error"
                $timeout(-> 
                    $scope.infostatus = false
                , 1500)
            else
                $scope.info = bd.info
                $timeout(->
                    $scope.hideModal()
                    $scope.infostatus = false
                , 1500)
                console.log bd.data
                $rootScope.global.uid = bd.data.uid
                $rootScope.global.username = bd.data.name
                $rootScope.global.dimen = bd.data.dimen
                # Ctrl
                $scope.infostyle = "alert-success"
                $scope.user = {}
                $rootScope.logstatus = true
    $scope.logout = ->
        logout((data)->
            $rootScope.global.dimen = ""
            $rootScope.global.username = ""
            $rootScope.global.uid = ""
            $rootScope.logstatus = false)
    $scope.opt = 
        backdropFade: true
        dialogFade: true
    ])
.controller('usersetCtrl', ['$scope', '$rootScope', '$location', 'showMsg', '$http', ($scope, $rootScope, $location, showMsg, $http) ->
    if !$rootScope.logstatus
        $location.path("/illust")
    $scope.setFile = (e) ->
        console.log(e)
    $scope.chdimen = (data)->
        $http.get("/member/cinfo/data/#{data}").success (cb)->
            if cb.status == 1
                showMsg "修改成功", 3500, 'wb'
            else 
                showMsg "修改失败", 3500, 'wb'
    ])
